package com.whw.fy.service.impl;

import com.whw.fy.entity.Message;
import com.whw.fy.entity.Student;
import com.whw.fy.mapper.*;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.whw.fy.entity.Comment;
import com.whw.fy.service.CommentService;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private ClazzMapper clazzMapper;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private ParentMapper parentMapper;
    @Resource
    private StudentMapper studentMapper;

    @Override
    public Message deleteByPrimaryKey(Integer commId) {
        return commentMapper.deleteByPrimaryKey(commId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insert(Comment record) {
        return commentMapper.insert(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(Comment record) {
        return commentMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer commId, Integer userId) {
        Comment comment = commentMapper.selectByPrimaryKey(commId);
        if (comment.getCommReviewers() == userId || comment.getCommByReviewers() == userId) {
            return Message.success(null).add(comment);
        } else {
            return Message.fail("用户没有资格查看此条评价");
        }
    }

    @Override
    public Message selectByPrimaryKeyForAdmin(Integer commId) {
        Comment comment = commentMapper.selectByPrimaryKey(commId);
        return Message.success(null).add(comment);
    }

    @Override
    public Message updateByPrimaryKeySelective(Comment record) {
        return commentMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateByPrimaryKey(Comment record) {
        return commentMapper.updateByPrimaryKey(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectAll(Integer userId) {
        System.out.println(userId);
        List<Comment> commentList = commentMapper.selectAllByCommByReviewers(userId);
        for (Comment comment:commentList){
            if (comment.getCommClassId()!=null)
                comment.setCommClassName(clazzMapper.selectByPrimaryKey(comment.getCommClassId()).getClassName());
            String[] types = comment.getCommType().split(";");
            switch (types[0]){
                case "4":
                    comment.setCommReviewersName(clazzMapper.selectByPrimaryKey(comment.getCommClassId()).getClassName());
                    break;
                case "3":
                    comment.setCommReviewersName(staffMapper.selectByPrimaryKey(comment.getCommReviewers()).getStaffName());
                    break;
                case "2":
                    comment.setCommReviewersName(parentMapper.selectByPrimaryKey(comment.getCommReviewers()).getParentName());
                    break;
                case "1":
                    comment.setCommReviewersName(studentMapper.selectByPrimaryKey(comment.getCommReviewers()).getStudentName());
                    break;
            }
            switch (types[1]){
                case "4":
                    comment.setCommReviewersName(clazzMapper.selectByPrimaryKey(comment.getCommClassId()).getClassName());
                    break;
                case "3":
                    comment.setCommByReviewersName(staffMapper.selectByPrimaryKey(comment.getCommByReviewers()).getStaffName());
                    break;
                case "2":
                    comment.setCommByReviewersName(parentMapper.selectByPrimaryKey(comment.getCommByReviewers()).getParentName());
                    break;
                case "1":
                    comment.setCommByReviewersName(studentMapper.selectByPrimaryKey(comment.getCommByReviewers()).getStudentName());
                    break;
            }

        }
        return Message.success(null).add(commentList);
    }

    @Override
    public Message selectAllForAdmin() {
        List<Comment> commentList = commentMapper.selectAll();
        for (Comment comment:commentList){
            if (comment.getCommClassId()!=null)
                comment.setCommClassName(clazzMapper.selectByPrimaryKey(comment.getCommClassId()).getClassName());
            String[] types = comment.getCommType().split(";");
            switch (types[0]){
                case "4":
                    comment.setCommReviewersName(clazzMapper.selectByPrimaryKey(comment.getCommClassId()).getClassName());
                    break;
                case "3":
                    comment.setCommReviewersName(staffMapper.selectByPrimaryKey(comment.getCommReviewers()).getStaffName());
                    break;
                case "2":
                    comment.setCommReviewersName(parentMapper.selectByPrimaryKey(comment.getCommReviewers()).getParentName());
                    break;
                case "1":
                    comment.setCommReviewersName(studentMapper.selectByPrimaryKey(comment.getCommReviewers()).getStudentName());
                    break;
            }
            switch (types[1]){
                case "4":
                    comment.setCommReviewersName(clazzMapper.selectByPrimaryKey(comment.getCommClassId()).getClassName());
                    break;
                case "3":
                    comment.setCommByReviewersName(staffMapper.selectByPrimaryKey(comment.getCommByReviewers()).getStaffName());
                    break;
                case "2":
                    comment.setCommByReviewersName(parentMapper.selectByPrimaryKey(comment.getCommByReviewers()).getParentName());
                    break;
                case "1":
                    comment.setCommByReviewersName(studentMapper.selectByPrimaryKey(comment.getCommByReviewers()).getStudentName());
                    break;
            }

        }
        return Message.success(null).add(commentList);
    }

    @Override
    public Comment selectByPrimaryKey(Integer commId) {
        return commentMapper.selectByPrimaryKey(commId);
    }

    @Override
    public int batchInsert(List<Comment> list) {
        return commentMapper.batchInsert(list);
    }

    @Override
    public Message selectAllByAll(Comment comment1) {
        List<Comment> commentList = commentMapper.selectByAll(comment1);
        for (Comment comment:commentList){
            if (comment.getCommClassId()!=null)
                comment.setCommClassName(clazzMapper.selectByPrimaryKey(comment.getCommClassId()).getClassName());
            String[] types = comment.getCommType().split(";");
            switch (types[0]){
                case "3":
                    comment.setCommReviewersName(staffMapper.selectByPrimaryKey(comment.getCommReviewers()).getStaffName());
                    break;
                case "2":
                    comment.setCommReviewersName(parentMapper.selectByPrimaryKey(comment.getCommReviewers()).getParentName());
                    break;
                case "1":
                    comment.setCommReviewersName(studentMapper.selectByPrimaryKey(comment.getCommReviewers()).getStudentName());
                    break;
            }
            switch (types[1]){
                case "3":
                    comment.setCommByReviewersName(staffMapper.selectByPrimaryKey(comment.getCommByReviewers()).getStaffName());
                    break;
                case "2":
                    comment.setCommByReviewersName(parentMapper.selectByPrimaryKey(comment.getCommByReviewers()).getParentName());
                    break;
                case "1":
                    comment.setCommByReviewersName(studentMapper.selectByPrimaryKey(comment.getCommByReviewers()).getStudentName());
                    break;
            }

        }
        return Message.success(null).add(commentList);
    }
}


